package com.xl.rentkeeper.act;

import android.app.Dialog;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.xl.rentkeeper.App;
import com.xl.rentkeeper.R;
import com.xl.rentkeeper.business.LogUploadLogic;
import com.xl.rentkeeper.util.Config;
import com.xl.rentkeeper.util.DialogUtil;
import com.xl.rentkeeper.util.TimeUtils;
import com.xl.rentkeeper.view.TimePopupWindow;
import com.xl.rentkeeper.view.TitleBar;

import java.util.Calendar;
import java.util.Date;

/**
 * Created by Administrator on 2015/9/2.
 */
public class LogUploadActivity extends BaseAct implements View.OnClickListener {
    private TextView mTVBeginTime;
    private TextView mTVEndTime;
    private TimePopupWindow pwTime;
    private Date mBeginDate = new Date();
    private long mBeginTime;
    private Date mEndDate = new Date();
    private long mEndTime;
    private Button mBTSubmit;
    private Dialog mDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_log_upload);
        TitleBar tb = (TitleBar) findViewById(R.id.tb);
        setTranslucentStatus();
        initTitleBar(tb);
        mTVBeginTime = (TextView) findViewById(R.id.tv_beginTime);
        mTVEndTime = (TextView) findViewById(R.id.tv_endTime);
        mTVBeginTime.setOnClickListener(this);
        mBTSubmit = (Button) findViewById(R.id.btn_submit);
        mBTSubmit.setOnClickListener(this);
        mTVEndTime.setOnClickListener(this);
    }
    private void setBeginTime() {
        //时间选择器
        pwTime = new TimePopupWindow(this, TimePopupWindow.Type.YEAR_MONTH_DAY_HOUR);
        //时间选择后回调
        pwTime.setOnTimeSelectListener(new TimePopupWindow.OnTimeSelectListener() {

            @Override
            public void onTimeSelect(Date date) {
                mTVBeginTime.setText(TimeUtils.fmtDate(date.getTime()));
                Calendar c = Calendar.getInstance();
                c.setTime(date);
                c.set(Calendar.MINUTE, 0);
                c.set(Calendar.SECOND, 0);
                date = c.getTime();
                mBeginDate = date;
                mBeginTime = date.getTime();
            }
        });
    }

    private void setEndTime() {
        //时间选择器
        pwTime = new TimePopupWindow(this, TimePopupWindow.Type.YEAR_MONTH_DAY_HOUR);
        //时间选择后回调
        pwTime.setOnTimeSelectListener(new TimePopupWindow.OnTimeSelectListener() {

            @Override
            public void onTimeSelect(Date date) {
                mTVEndTime.setText(TimeUtils.fmtDate(date.getTime()));
                Calendar c = Calendar.getInstance();
                c.setTime(date);
                c.set(Calendar.MINUTE, 0);
                c.set(Calendar.SECOND, 0);
                date= c.getTime();
                mEndDate = date;
                mEndTime = date.getTime();
            }
        });
    }

    @Override
    public String[] monitorEvents() {
        return new String[]{LogUploadLogic.CMD_UPLOAD_LOG};
    }

    @Override
    public void onEvent(String event, boolean ret, String errInfom, Object[] data) {
        super.onEvent(event, ret, errInfom, data);
        if (ret ){
            if(mDialog!=null && mDialog.isShowing())mDialog.dismiss();
            App.showToast("上传成功");
        }else {
            if(mDialog!=null && mDialog.isShowing())mDialog.dismiss();
            App.showToast(errInfom);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_beginTime:
                setBeginTime();
                pwTime.showAtLocation(mTVBeginTime, Gravity.BOTTOM, 0, 0,mBeginDate);
                break;

            case R.id.tv_endTime:
                setEndTime();
                pwTime.showAtLocation(mTVEndTime, Gravity.BOTTOM, 0, 0, mEndDate);
                break;

            case R.id.btn_submit:
                submit();
                break;
        }
    }

    private void submit() {
        if(mBeginTime==0){
            App.showToast("请填写开始时间");
            return;
        }
        if(mEndTime==0){
            App.showToast("请填写结束时间");
            return;
        }
        if(mEndTime-mBeginTime<0){
            App.showToast("开始时间必须在结束时间之前");
            return;
        }
        if(mEndTime-mBeginTime> Config.LOG_TIME_MILL){
            App.showToast("间隔不能超过"+Config.LOG_TIME+"小时");
            return;
        }
        mDialog = DialogUtil.showProgressDialog(this,R.string.loading);
        LogUploadLogic.getInstance().uploadLog(mBeginTime,mEndTime);
    }
}
